BikeList.tsx ➔ BikeList   B
last analyzed

Complexity

Conditions 2

Size

Total Lines 67
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 63
dl 0
loc 67
ccs 6
cts 8
cp 0.75
crap 2.0625
rs 8.2109
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import { useEffect, useState, useContext } from 'react';
2
import { Scooter } from '../helpers/bike-functions';
3
import { Badge } from 'flowbite-react'
4
import RealTimeContext from '../helpers/RealTimeContext';
5
import { formatTimestamp } from '../helpers/other-functions';
6
7
type Props = {
8
    scooterData: Scooter[];
9
    isCityList: boolean,
10
    isLowRes?: boolean
11
}
12
13
export default function BikeList( {scooterData, isCityList} : Props) {
14
15 1
    const [scootersString, setScootersString] = useState("")
16 1
    const { isLowRes } = useContext(RealTimeContext);
17
18 1
    useEffect(() => {
19 1
        const scooterDataString = scooterData.map(scooter => 
20
        Object.values(scooter).filter(value => typeof value !== 'object').join(' | ')
21
            ).join('<br />');
22 1
        setScootersString(scooterDataString);
23
    },[scooterData]);
24
    
25 2
  return !isLowRes ? (
26
  <ul className="w-full sm:max-w-4xl mx-auto" data-testid="bikelist">
27
    {scooterData.map((scooter) => (
28
        <li key={scooter.id} className="flex flex-col w-full flex-nowrap justify-between gap-4 p-4 mb-6 bg-gray-100 rounded-lg
29
                shadow-md sm:flex-row sm:items-center">
30
                <div>
31
                    <div className="flex items-center p-1 rounded-lg">
32
                        <span className="font-semibold">id:</span>
33
                        <Badge>{scooter.id}</Badge>
34
                    </div>
35
                    { isCityList &&
36
                        <div className="flex items-center p-1 rounded-lg">
37
                            <span className="font-semibold">city:</span>
38
                            <Badge>{scooter.city}</Badge>
39
                        </div>
40
                    }
41
                    <div className="flex items-center p-1 rounded-lg">
42
                        <span className="font-semibold">createdAt:</span>
43
                        <Badge>{formatTimestamp(scooter.createdAt)}</Badge>
44
                    </div>
45
46
                    <div className="flex items-center p-1 rounded-lg">
47
                        <span className="font-semibold">updatedAt:</span>
48
                        <Badge>{formatTimestamp(scooter.updatedAt)}</Badge>
49
                    </div>
50
                </div>
51
52
                <div>
53
                    <div className="flex items-center p-1 rounded-lg">
54
                        <span className="font-semibold">batteryLevel:</span>
55
                        <Badge>{scooter.batteryLevel}</Badge>
56
                    </div>
57
58
                    <div className="flex items-center p-1 rounded-lg">
59
                        <span className="font-semibold">status:</span>
60
                        <Badge>{scooter.status}</Badge>
61
                    </div>
62
63
                    <div className="flex items-center p-1 rounded-lg">
64
                        <span className="font-semibold">longitude:</span>
65
                        <Badge>{scooter.longitude}</Badge>
66
                    </div>
67
68
                    <div className="flex items-center p-1 rounded-lg">
69
                        <span className="font-semibold">latitude:</span>
70
                        <Badge>{scooter.latitude}</Badge>
71
                    </div>
72
                </div>
73
            </li>
74
            ))}
75
            </ul>
76
        ) :
77
        (
78
        <div dangerouslySetInnerHTML={{ __html: scootersString }} />
79
    )
80
}
81